package com.wu.creditmanager;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.wu.creditmanager.db.CreditsDbAdapter;
import com.wu.creditmanager.entity.Credit;
import com.wu.creditmanager.util.ImageUtil;
import com.wu.creditmanager.util.LayoutUtil;

public class CreditInfo extends Activity {

	private Credit credit;
	private CreditsDbAdapter mDbHelper;
	private static final int ACTIVITY_EDIT = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.credit_info);
		mDbHelper = new CreditsDbAdapter(this);
		Bundle extras = getIntent().getExtras();
		long creditId = extras.getLong(Credit.TAG);
		mDbHelper.open();
		credit = mDbHelper.fetchCredit(creditId);
		mDbHelper.close();
		LayoutUtil.initToolbar(this, credit.getName(), null, getResources()
				.getString(R.string.menu_edit), new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(CreditInfo.this, CreditEditor.class);
				intent.putExtra(Credit.TAG, credit);
				startActivityForResult(intent, ACTIVITY_EDIT);
			}
		});
		populateFields();
	}

	private void populateFields() {
		ImageUtil.setImage(credit.getIcon(),
				(ImageView) findViewById(R.id.l_info_id_image));
		((TextView) findViewById(R.id.l_info_id_cname)).setText(credit
				.getName());
		((TextView) findViewById(R.id.l_info_id_cbank)).setText(credit
				.getBank().getName());
		((TextView) findViewById(R.id.l_info_id_clast4code)).setText(credit
				.getLast4code());
		((TextView) findViewById(R.id.l_info_id_cstatementday)).setText(credit
				.getStatementDay() + "");
		((TextView) findViewById(R.id.l_info_id_rmblimit)).setText(credit
				.getLimitRMB() + "");
		((TextView) findViewById(R.id.l_info_id_usdlimit)).setText(credit
				.getLimitUSD() + "");
		((TextView) findViewById(R.id.l_info_id_paymentday)).setText(credit
				.getPaymentText(this));
		((TextView) findViewById(R.id.l_info_id_recentlypaymentday))
				.setText(new SimpleDateFormat("M-d").format(new Date(credit
						.getNextAlarmTime().getTimeInMillis())));

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case ACTIVITY_EDIT:
			Bundle extras = data.getExtras();
			Credit credit = (Credit) extras.getSerializable(Credit.TAG);
			this.credit = credit;
			populateFields();
			mDbHelper.open(true);
			mDbHelper.updateCredit(credit.getId(), credit);
			mDbHelper.close();
			AlarmListWidget.update(this);
			break;
		default:
			break;
		}
	}
}
